Ext.define('Xyh.controller.Orders', {
	extend : 'Ext.app.Controller',
	models : ['Order', 'Bill'],
	stores : ['Order'],
	views : ['orders.CreateOrder', 'orders.List', 'orders.Hbl', 'orders.Mbl',
			'orders.AddBill', 'orders.EditOrder'],
	init : function() {
		this.control({
					'inputorder button[action=save]' : {
						click : this.addOrder
					},
					'orderlist gridpanel' : {
						itemdblclick : this.editOrder
					},
					'editorder button[action=save]' : {
						click : this.saveOrder
					},
					'combobox[name=client_code]' : {
						select : this.completeCustomer
					},
					'combobox[name=cargo_collection_type]' : {
						select : this.showProxy
					},
					'combobox[name=cargo_collection_code]' : {
						select : this.completeProxy
					},
					'combobox[name=shipperCombobox]' : {
						select : this.completeShipper
					},
					'combobox[name=consigneeCombobox]' : {
						select : this.completeConsignee
					},
					'combobox[name=notifierCombobox]' : {
						select : this.completeNotifier
					},
					'combobox[name=containerCombobox]' : {
						select : this.addContainer
					},
					'button[action=delcontainer]' : {
						click : this.delcontainer
					},
					'numberfield[action=changeContainer]' : {
						change : this._changeContainer
					},
					'combobox[name=shipping_company_code]' : {
						select : this.completeShippingCompany
					},
					'combobox[name=booking_proxy_company_code]' : {
						select : this.completeBookingProxy
					},
					'orderlist button[action=search]' : {
						click : this.search
					},
					'orderlist button[action=reset_search]' : {
						click : this.resetSearch
					},
					'hbl button[action=save]' : {
						click : this.updateHbl
					},
					'mbl button[action=save]' : {
						click : this.updateMbl
					},
					'billadd combobox[name=bill_code]' : {
						change : this.completeFeetype
					},
					'billadd button[action=save]' : {
						click : this.addBill
					}
				});

	},
	addOrder : function(button) {
		var form = button.up('inputorder').down('form').getForm();
		if (form.isValid()) {
			form.submit({
						success : function(form, action) {
							Ext.Msg.alert('OK', '添加成功');
							form.reset();
						},
						failure : function(form, action) {
							Ext.Msg.alert('ERROR', '添加失败');
						}
					});
		}
	},
	saveOrder : function(button) {
		var form = button.up('editorder').down('form').getForm();
		if (form.isValid()) {
			form.submit({
						success : function(form, action) {
							Ext.Msg.alert('OK', '修改委拖单成功');
						},
						failure : function(form, action) {
							Ext.Msg.alert('ERROR', '修改委拖单失败');
						}
					});
		}
	},
	editOrder : function(grid, record) {
		var tab_main = Ext.getCmp('tab_Main');
		var tab = tab_main.child('#order' + record.get('id'));
		if (tab == undefined) {
			var config = {
				title : '编辑' + record.get('order_code'),
				id : 'order' + record.get('id'),
				closable : true,
				padding : 5,
				items : {
					xtype : 'editorder',
					orderid : record.get('id')
				}
			};
			var tab = tab_main.add(config);
			var form = tab.down('form');
			Ext.Ajax.request({
				url : '/order/' + record.get('id'),
				success : function(response) {
					var values = Ext.decode(response.responseText);
					form.getForm().setValues(values.fields);
					form.down('hiddenfield').setValue(values.pk);

					// 初始化代理
					var type = values.fields.cargo_collection_type;
					var parentPanel = form.down('panel');
					if (type == 1) {
						if (parentPanel
								.down('combobox[name=cargo_collection_code]') != null) {
							parentPanel
									.remove(parentPanel
											.down('combobox[name=cargo_collection_code]'));
							parentPanel
									.remove(parentPanel
											.down('textfield[name=cargo_collection_name]'));
						}
					} else {
						if (parentPanel
								.down('combobox[name=cargo_collection_code]') == null) {
							parentPanel.add([{
								xtype : 'combobox',
								id : 'editcargocollectioncode' + record.get('id'),
								fieldLabel : '指定代理',
								name : 'cargo_collection_code',
								queryMode : 'local',
								forceSelection : true,
								displayField : 'code',
								valueField : 'code',
								allowBlank : false,
								blankText : '指定代理为空！',
								store : {
									fields : ['code', 'name', 'person',
											'phone', 'fax'],
									autoLoad : true,
									orderid : record.get('id'),
									proxy : {
										type : 'ajax',
										url : '/contact/list?oftype=hwdl',
										reader : {
											type : 'json',
											root : 'contacts'
										}
									},
									listeners : {
										load : {
											fn : function(store) {
												Ext
														.getCmp('editcargocollectioncode'
																+ this.orderid)
														.setValue(values.fields.cargo_collection_code);
											}
										}
									}
								}
							}, {
								xtype : 'textfield',
								name : 'cargo_collection_name',
								labelSeparator : '',
								fieldLabel : ' ',
								readOnly : true,
								value : values.fields.cargo_collection_name
							}]);

						}
					}

				}
			});
		}
		tab_main.setActiveTab(tab);

	},
	completeCustomer : function(combobox, records) {
		var form = combobox.up('form');
		var contactStore = combobox.store;
		var newValue = records[0].get('code');
		var newRecord = contactStore.findRecord('code', newValue);
		var client_company = form.down('textfield[name=client_company]');
		client_company.setValue(newRecord.get('name'));
		var client_person = form.down('textfield[name=client_person]');
		client_person.setValue(newRecord.get('person'));
		var client_phone = form.down('textfield[name=client_phone]');
		client_phone.setValue(newRecord.get('phone'));
		var client_fax = form.down('textfield[name=client_fax]');
		client_fax.setValue(newRecord.get('fax'));

	},
	showProxy : function(combobox, records) {
		var parentPanel = combobox.findParentByType('panel');
		var newValue = records[0].get('field1');
		if (newValue == 1) {
			if (parentPanel.down('combobox[name=cargo_collection_code]') != null) {
				parentPanel.remove(parentPanel
						.down('combobox[name=cargo_collection_code]'));
				parentPanel.remove(parentPanel
						.down('textfield[name=cargo_collection_name]'));
			}
		} else {
			if (parentPanel.down('combobox[name=cargo_collection_code]') == null) {
				parentPanel.add([{
							xtype : 'combobox',
							fieldLabel : '指定代理',
							name : 'cargo_collection_code',
							queryMode : 'local',
							forceSelection : true,
							displayField : 'code',
							valueField : 'code',
							allowBlank : false,
							blankText : '指定代理为空！',
							store : {
								fields : ['code', 'name', 'person', 'phone',
										'fax'],
								autoLoad : true,
								proxy : {
									type : 'ajax',
									url : '/contact/list?oftype=jndl',
									reader : {
										type : 'json',
										root : 'contacts'
									}
								}
							}
						}, {
							xtype : 'textfield',
							name : 'cargo_collection_name',
							labelSeparator : '',
							fieldLabel : ' ',
							readOnly : true,
							value : ''
						}]);
			}
		}

	},
	completeProxy : function(combobox, records) {
		var form = combobox.up('form');
		var newValue = records[0].get('code');
		var proxyStore = combobox.store;
		var newRecord = proxyStore.findRecord('code', newValue);
		var cargo_collection_name = form
				.down('textfield[name=cargo_collection_name]');
		cargo_collection_name.setValue(newRecord.get('name'));
	},
	completeShipper : function(combobox, records) {
		var form = combobox.up('form');
		var newValue = records[0].get('code');
		var store = combobox.store;
		var newRecord = store.findRecord('code', newValue);
		var shipper = form.down('textareafield[name=shipper]');
		shipper.setValue(newRecord.get('name') + '\r\n'
				+ newRecord.get('address'));
	},
	completeConsignee : function(combobox, records) {
		var form = combobox.up('form');
		var store = combobox.store;
		var newValue = records[0].get('code');
		var newRecord = store.findRecord('code', newValue);
		var consignee = form.down('textareafield[name=consignee]');
		consignee.setValue(newRecord.get('name') + '\r\n'
				+ newRecord.get('address'));
	},
	completeNotifier : function(combobox, records) {
		var form = combobox.up('form');
		var store = combobox.store;
		var newValue = records[0].get('code');
		var newRecord = store.findRecord('code', newValue);
		var notifier = form.down('textareafield[name=notifier]');
		notifier.setValue(newRecord.get('name') + '\r\n'
				+ newRecord.get('address'));
	},
	changeContainer : function(component) {
		var container_info = component.down('textfield[name=container_info]');
		var selectedContainer = component.query('numberfield');
		var result = '';
		Ext.each(selectedContainer, function(_container) {
					result += (_container.fieldLabel + '×' + _container.value + ";");
				});
		container_info.setValue(result);
	},
	addContainer : function(combobox, records) {
		var parentPanel = combobox.findParentByType('panel');
		var newValue = records[0].get('id');
		if (parentPanel.down('#numbercontainer' + newValue) == null) {
			parentPanel.insert(1, {
						id : 'numbercontainer' + newValue,
						xtype : 'numberfield',
						editable : false,
						fieldLabel : combobox.store.findRecord('id', newValue)
								.get('size'),
						value : 1,
						maxValue : 99,
						minValue : 1,
						action : 'changeContainer'
					});
			parentPanel.insert(2, {
						xtype : 'button',
						delcontainerid : 'numbercontainer' + newValue,
						action : 'delcontainer',
						text : '删除'
					})
			this.changeContainer(parentPanel);
		}
	},
	delcontainer : function(button) {
		var parentPanel = button.findParentByType('panel');
		if (parentPanel.down('#' + button.delcontainerid) != null) {
			parentPanel.remove(button.delcontainerid);
			parentPanel.remove(button);
			this.changeContainer(parentPanel);
		}
	},
	_changeContainer : function(button) {
		var parentPanel = button.findParentByType('panel');
		this.changeContainer(parentPanel);
	},
	completeShippingCompany : function(combobox, records) {
		var form = combobox.up('form');
		var newValue = records[0].get('code');
		var store = combobox.store;
		var newRecord = store.findRecord('code', newValue);
		var shipping_company_name = form
				.down('textfield[name=shipping_company_name]');
		shipping_company_name.setValue(newRecord.get('name'));
		var shipping_company_person = form
				.down('textfield[name=shipping_company_person]');
		shipping_company_person.setValue(newRecord.get('person'));
		var shipping_company_phone = form
				.down('textfield[name=shipping_company_phone]');
		shipping_company_phone.setValue(newRecord.get('phone'));
		var shipping_company_fax = form
				.down('textfield[name=shipping_company_fax]');
		shipping_company_fax.setValue(newRecord.get('fax'));

	},
	completeBookingProxy : function(combobox, records) {
		var form = combobox.up('form');
		var store = combobox.store;
		var newValue = records[0].get('code');
		var newRecord = store.findRecord('code', newValue);
		var booking_proxy_company_name = form
				.down('textfield[name=booking_proxy_company_name]');
		booking_proxy_company_name.setValue(newRecord.get('name'));
		var booking_proxy_company_person = form
				.down('textfield[name=booking_proxy_company_person]');
		booking_proxy_company_person.setValue(newRecord.get('person'));
		var booking_proxy_company_phone = form
				.down('textfield[name=booking_proxy_company_phone]');
		booking_proxy_company_phone.setValue(newRecord.get('phone'));
		var booking_proxy_company_fax = form
				.down('textfield[name=booking_proxy_company_fax]');
		booking_proxy_company_fax.setValue(newRecord.get('fax'));

	},
	search : function(button) {
		var form = button.up('form');
		var params = form.getValues();
		var store = Ext.getStore('Order');
		store.proxy.extraParams = params;
		store.loadPage(1);
	},
	resetSearch : function(button) {
		var form = button.up('form');
		form.getForm().reset();
	},
	updateHbl : function(button) {
		var form = button.up('hbl').down('form').getForm();
		if (form.isValid()) {
			form.submit({
						success : function(form, action) {
							Ext.Msg.alert('OK', '更新成功');

						},
						failure : function(form, action) {
							Ext.Msg.alert('ERROR', '更新HB/L失败');
						}
					});
		}
	},
	updateMbl : function(button) {
		var form = button.up('mbl').down('form').getForm();
		if (form.isValid()) {
			form.submit({
						success : function(form, action) {
							Ext.Msg.alert('OK', '更新成功');

						},
						failure : function(form, action) {
							Ext.Msg.alert('ERROR', '更新MB/L失败');
						}
					});
		}
	},
	completeFeetype : function(combobox, newValue, oldValue) {
		var feetypeName = combobox.up('form').down('textfield[name=bill_name]');
		var store = combobox.store;
		var newRecord = store.findRecord('code', newValue);
		feetypeName.setValue(newRecord.get('name'));
	},
	addBill : function(button) {
		var form = button.up('billadd').down('form').getForm();
		var win = button.up('window');
		if (form.isValid()) {
			form.submit({
						success : function(form, action) {
							Ext.Msg.alert('OK', '添加成功');
							button.up('billadd').cbutton.store.load();
							win.close();
						},
						failure : function(form, action) {
							Ext.Msg.alert('ERROR', '添加失败');
						}
					});
		}
	}
})